<?php
declare (strict_types=1);

namespace app\common\model\payment\admin;

use app\common\model\BaseModel;
use app\common\model\DeviceType;
use app\course\model\base\Course;
use app\mall\model\product\admin\Product;
use app\user\model\user\Account;
use think\model\concern\SoftDelete;
use think\model\relation\BelongsTo;
use think\model\relation\HasOne;

/**
 * 订单模型
 */
class PaymentOrderItem extends BaseModel
{
    use SoftDelete;

    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'payment_order_item';

    protected $type = [
        'credit'   => 'array',
        'ext_data' => 'array',
    ];

    public function pdorder(): BelongsTo
    {
        return $this->belongsTo(PaymentOrder::class, 'order_no', 'order_no')->bind(['status', 'status_text']);
    }

    /**
     * 订单来源
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 2020/07/04
     *
     * @param $val
     * @param $data
     * @return mixed|string
     */
    public function getFromTextAttr($val, $data)
    {
        $text = $this->getFromTextInfo();

        return $text[$data['from']] ?? '未知';
    }

    protected function getFromTextInfo(): array
    {
        return DeviceType::getDeviceList();
    }

    /**
     * 获取来源选项数据
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-04
     *
     */
    public function getFromOptions(): array
    {
        $options = [];

        $from = $this->getFromTextInfo();
        foreach ($from as $key => $value) {
            $options[] = ['title' => $value, 'value' => $key];
        }

        return $options;
    }

    /**
     * 付费用户信息
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-17
     *
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(Account::class, 'user_id');
    }

    /**
     * 交易记录
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-04
     */
    public function transaction()
    {
        return $this->hasOne(PaymentTransaction::class, 'order_no', 'order_no');
    }

    /**
     * userName
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/02/14
     *
     * @return BelongsTo
     */
    public function userName(): BelongsTo
    {
        return $this->belongsTo(Account::class, 'user_id')->removeOption('soft_delete')->removeWhereField('mhm_id')->bind(['user_name']);
    }

    /**
     * paymentInfo
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/02/14
     *
     * @return HasOne
     */
    public function paymentInfo(): HasOne
    {
        return $this->hasOne(PaymentTransaction::class, 'order_no', 'order_no')->bind(['payment_time', 'payment_type_text']);
    }

    /**
     * courseTitle
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/02/14
     *
     * @return BelongsTo
     */
    public function courseTitle(): BelongsTo
    {
        return $this->belongsTo(Course::class, 'product_id')->bind(['product_title' => 'title'])->removeOption('soft_delete');
    }

    /**
     * 商品
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/11/28
     *
     * @return BelongsTo
     */
    public function product(): BelongsTo
    {
        return $this->belongsTo(Product::class, 'product_id')->bind(['title', 'cash_price', 'credit_price', 'fictitious', 'carriage']);
    }

    /**
     * 主订单
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/11/28
     *
     * @return BelongsTo
     */
    public function main(): BelongsTo
    {
        return $this->belongsTo(PaymentOrder::class, 'order_no', 'order_no')->bind(['status', 'payment_time', 'addrinfo', 'status_text']);
    }

    /**
     * username
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/11/28
     *
     * @return BelongsTo
     */
    public function uname(): BelongsTo
    {
        return $this->belongsTo(Account::class, 'user_id')->bind(['user_name']);
    }

}
